package model;

import dao.GetCnn;
import entity.danhmuc;
import java.sql.*;
import java.util.List;
import java.util.ArrayList;
import dao.common;

public class DanhmucModel {
	public List<danhmuc> getlistdanhmuc() throws  SQLException{
		List<danhmuc> list = new ArrayList<danhmuc>();
		String select = "select * from DANHMUC";
		Connection cnn = GetCnn.getCnn();
		Statement stat = cnn.createStatement();
		ResultSet rs = stat.executeQuery(select);
		while (rs.next()){
			danhmuc entity = new danhmuc();
			entity.setMadanhmuc(rs.getString("MADANHMUC"));
			entity.setTendanhmuc(rs.getString("TENDANHMUC"));
			list.add(entity);
		}
		return list;
	}
	
	public List<danhmuc> timdanhmuc(String strSearch) throws SQLException{
		List<danhmuc> listdm = new ArrayList<danhmuc>();   	   	
    	Connection cnn = GetCnn.getCnn();
		Statement stat = cnn.createStatement();
		
		ResultSet rs = stat.executeQuery(strSearch);
		while (rs.next()){
			danhmuc entity = new danhmuc();
			entity.setMadanhmuc(rs.getString("MADANHMUC"));
			entity.setTendanhmuc(rs.getString("TENDANHMUC"));
			listdm.add(entity);
		}
		return listdm;
    }
	
	
	public String getOptions(boolean isSearch, boolean isRequired, String selectedValue) {   

	    String sOptions = "";   
	    String sSel = "";   

	    if (isSearch) {   
	        sOptions += "<option value=\"\">All</option>";   
	    } else {   
	        if (!isRequired) {   
	            sOptions += "<option value=\"\"></option>";   
	        }   
	    }   
	    try {   
	    	String select = "select * from DANHMUC";
			Connection cnn = GetCnn.getCnn();
			Statement stat = cnn.createStatement();
			ResultSet rs = stat.executeQuery(select); 
	        while (rs.next()) {   
	            String id =common.toHTML(rs.getString(1));
	            String val = common.toHTML(rs.getString(2));   
	            if (id.compareTo(selectedValue) == 0) {   
	                sSel = "SELECTED";   
	            } else {   
	                sSel = "";   
	            }   
	            sOptions += "<option value=\"" + id + "\" " + sSel + ">" + val   
	                    + "</option>";   
	        }   
	        rs.close();   
	        stat.close();   
	    } catch (Exception e) {   
	    }   
	    return sOptions;   
	}

}
